/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import configuration.CSS;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import model.Charter;
import model.Issue;
import model.Session;
import org.apache.poi.ss.formula.eval.NotImplementedException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 *
 * @author heitor
 */
public class SessionDaoXML implements ISessionDAO {

    @Override
    public void saveSession(Session session, String filename) throws FileNotFoundException {
        PrintWriter out = null;
        try {
            String str = session.toString();
            out = new PrintWriter(filename);
            //System.out.println(str);
            out.print(str);
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
        }
    }

    public Session loadSession(String filename) throws IOException{
        return new Session(new Charter());
    }

    public void createReport(Session session, String filename) {
        try {

            
            
            DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
            Document document = documentBuilder.newDocument();
            
            Node html = document.createElement("html");
            Node style = document.createElement("style");
            html.appendChild(style);
            Node body = document.createElement("body");
            html.appendChild(body);
            Node wrapper = document.createElement("div");
            body.appendChild(wrapper);
            Node titleH1 = document.createElement("h1");
            wrapper.appendChild(titleH1);
            Node divInfo = document.createElement("div");
            wrapper.appendChild(divInfo);
            Node productP = document.createElement("p");
            divInfo.appendChild(productP);
            Node testerP = document.createElement("p");
            divInfo.appendChild(testerP);
            Node durationP = document.createElement("p");
            divInfo.appendChild(durationP);
            //Node dateP = document.createElement("p");
            //divInfo.appendChild(dateP);
            wrapper.appendChild(document.createElement("hr"));
            Node divContent = document.createElement("div");
            wrapper.appendChild(divContent);
            Node failsList = document.createElement("ul");
            divContent.appendChild(failsList);
            
            List<Issue> list = session.getIssues();
            for(Issue i : list){
                Node li = document.createElement("li");
                li.setTextContent(i.getName() + " - " + i.getDescription() + "<br /> - Issue type: " + i.getType());
                failsList.appendChild(li);
            }
            divContent.appendChild(document.createElement("hr"));
            
            Node divComments = document.createElement("div");
            divContent.appendChild(divComments);
            
            document.appendChild(html);
            
            style.setTextContent(CSS.CSS);
            
            DateFormat df = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT, Locale.US);
            titleH1.setTextContent(session.getProduct().getName()+ " - " + df.format(new Date()));
            productP.setTextContent("Product: "+session.getProduct().getName());
//            testerP.setTextContent("Tester: "+session.getTesterField()+ " - " + session.getIdField());
//            durationP.setTextContent("Duration: "+session.getDurationField());
//            divComments.setTextContent(session.getCommentsField());
            
//            Node node = document.getElementsByTagName("html").item(0);
//
//            Element charterNode = document.createElement("charter");
//
//            Element etID = document.createElement("etId");
//            etID.setTextContent("teste");
//            charterNode.appendChild(etID);
//
//            Element name = document.createElement("name");
//            name.setTextContent(session.getCommentsField());
//            charterNode.appendChild(name);
//
//            node.appendChild(charterNode);
//            System.out.println(html.getTextContent());
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult streamResult = new StreamResult(new File(filename));
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(source, streamResult);

        } catch (ParserConfigurationException ex) {
            Logger.getLogger(CharterDaoXML.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (SAXException ex) {
//            Logger.getLogger(CharterDaoXML.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (IOException ex) {
//            Logger.getLogger(CharterDaoXML.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(SessionDaoXML.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(SessionDaoXML.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
